/*
 * This is Melet, a salary slip designer. Feed it with data regarding to salary payment, prepare it for print through the graphical presentation, and you have it done.
 * Copyright (C) 2014, AFDiaX
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This program is based on UMLet, a Free UML Tool for Fast UML Diagrams.
 * To contact the authors of UMLet visit https://code.google.com/p/umlet/people/list
 * To contact the authors of Melet visit https://gna.org/users/gabocze
 */
/**
 * 
 */
package com.baselet.gui;

import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.lang.reflect.InvocationTargetException;
import java.util.Map.Entry;

import javax.swing.JComponent;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.plaf.LayerUI;

import com.baselet.gui.helper.DecoratedJComponentTwins;

/**
 * @author Clara Yael Bradichansky
 * 
 */
public class MyRelationUI extends LayerUI<JComponent> {
	public JComponent leftTarget;
	public Component rightTarget;
	public DecoratedJComponentTwins drawIterator;
	public JComponent decoratedTarget;
	/* (non-Javadoc)
	 * @see javax.swing.plaf.LayerUI#paint(java.awt.Graphics, javax.swing.JComponent)
	 */
	@Override
	public void paint(Graphics g, JComponent c) {
		super.paint(g, c);

		Graphics2D g2 = (Graphics2D)g;
		// TODO Gabriel
		/*
		 * Dibujar conectores(Iterador[Punto(coordenada inicial), Punto(coordenada final)] coordenada inicial=f(x=Children de c), coordenada final=f'(Elemento(baselet)/JComponent correspondiente de x))
		 *   |-> Dibujar punta de flecha
		 * 
		 */
		// g.drawLine(0, 0, c.getWidth(), c.getHeight());
		//g.drawString( String.valueOf(((JTextField)((JPanel)((JScrollPane)((JSplitPane)leftTarget).getRightComponent()).getViewport().getView()).getComponent(5)).getVisibleRect().getY()),0, 60);
		//g.drawString( String.valueOf(((JPanel)((JScrollPane)((JSplitPane)leftTarget).getRightComponent()).getViewport().getView()).getY()),0, 80);
		//g.drawString( String.valueOf(((JPanel)((JScrollPane)((JSplitPane)leftTarget).getRightComponent()).getViewport().getView()).getVisibleRect().getY()),w/2, h/2);
		
		for(Entry<JComponent,JComponent>e:drawIterator) {
			//System.out.println("Draw Origin: " + e.getKey().getName() + ", End: " + e.getValue() + ".");
			int x=0;
			int y=0;
			JComponent c2 = e.getKey();
			do {
				x += c2.getX();
				y += c2.getY();
				c2 = (JComponent) c2.getParent();
			} while(c2!=c);
			
			int x2=0;
			int y2=0;
			c2 = e.getValue();
			do {
				x2 += c2.getX();
				y2 += c2.getY();
				c2 = (JComponent) c2.getParent();
			} while (c2!=c);
			
			// width and height are not translateable
			g2.drawLine(x, y, x2, y2);
		}
		
		g2.dispose();

	}
	/* (non-Javadoc)
	 * @see javax.swing.plaf.LayerUI#eventDispatched(java.awt.AWTEvent, javax.swing.JLayer)
	 */
	@Override
	public void eventDispatched(AWTEvent e, JLayer<? extends JComponent> l) {
		decoratedTarget.repaint();
//		Rectangle r = leftTarget.getComponent(1).getBounds();
//		r.translate(20,20);
//		leftTarget.getComponent(1).setBounds(r);
		super.eventDispatched(e, l);
//		System.out.println(leftTarget.getComponent(1).getName());
	}
	/* (non-Javadoc)
	 * @see javax.swing.plaf.LayerUI#installUI(javax.swing.JComponent)
	 */
	@Override
	public void installUI(JComponent c) {
		// TODO Auto-generated method stub
		super.installUI(c);
		((JLayer)c).setLayerEventMask(AWTEvent.COMPONENT_EVENT_MASK);
	}
	/* (non-Javadoc)
	 * @see javax.swing.plaf.LayerUI#uninstallUI(javax.swing.JComponent)
	 */
	@Override
	public void uninstallUI(JComponent c) {
		// TODO Auto-generated method stub
		super.uninstallUI(c);
		((JLayer)c).setLayerEventMask(0);
	}
	
	// TODO Gabriel probar con los installUI y uninstallUI de registrarse con ciertos eventos (keyboard)
	

}
